/**
 * [661] 图片平滑器
 *
 * @format
 * @lc app=leetcode.cn id=661 lang=javascript
 */

// @lc code=start
/**
 * @param {number[][]} img
 * @return {number[][]}
 */
var imageSmoother = function (img) {
    const result = []
    for (let i = 0; i < img.length; i++) {
        result.push([])
        for (let j = 0; j < img[0].length; j++) {
            let [x, y] = [j - 1, i - 1]
            let [sum, count] = [0, 0]
            for (let k = 0; k < 9; k++) {
                if (
                    img[y + Math.floor(k / 3)] != undefined &&
                    img[y + Math.floor(k / 3)][x + (k % 3)] != undefined
                ) {
                    sum += img[y + Math.floor(k / 3)][x + (k % 3)]
                    count++
                }
            }
            result[i].push(Math.floor(sum / count))
        }
    }
    return result
}
// debugger
// console.log(
//     imageSmoother([
//         [1, 1, 1],
//         [1, 0, 1],
//         [1, 1, 1]
//     ])
// )
// @lc code=end
